import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { TransactionDetailsService }  from './transaction-details.service';
import { LabelValueEntity } from './transaction-details.entity';
import Singleton from '../../services/singleton';

@Component({
  selector: 'app-transaction-details',
  templateUrl: './transaction-details.component.html',
  styleUrls: ['./transaction-details.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TransactionDetailsComponent implements OnInit {
  singleton_username: Singleton;
  username: string;

  isVisible: boolean = false;
  listOfOption: LabelValueEntity[] = [];
  selectedObject: string;
  operator_count: number;
  constructor(
    private readonly transactionDetailsService: TransactionDetailsService,
    private def:ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.singleton_username = Singleton.getInstance();
    this.username = this.singleton_username.getUserName();
    this.listOfOption = this.transactionDetailsService.getOptionalObject(this.username);
  }

  showModal(): void {
    if (this.operator_count === undefined) {
      this.selectedObject = '';
      return ;
    }
    this.isVisible = true;
  }

  handleSettingCancel(): void {
    this.isVisible = false;
  }

  handleSettingOk(): void {
    console.log(this.selectedObject, this.operator_count);
    this.transactionDetailsService.postNewRecipy(this.username, this.selectedObject, this.operator_count).subscribe(str => {
      if (str.state === true) {
        
      }
    });
    this.selectedObject = undefined;
    this.operator_count = 0;
    this.isVisible = false;
  }
}
